package com.atguigu.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Objects;

/**
 * className: FileChannelDemo3
 * description:
 * date: 2023/3/9 0009-11:59
 * <p>
 * project: nio
 * package: com.atguigu.channel
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class FileChannelDemo4 {

    /**
     * transferTo()
     *
     * @param args {@link String[]}
     * @throws IOException e
     */
    public static void main(String[] args) throws IOException {
        // 创建两个 fileChannel
        RandomAccessFile aFile = new RandomAccessFile(Objects.requireNonNull(FileChannelDemo4.class.getClassLoader().getResource("001.txt")).getPath(), "rw");
        FileChannel fromChannel = aFile.getChannel();

        RandomAccessFile bFile = new RandomAccessFile(Objects.requireNonNull(FileChannelDemo4.class.getClassLoader().getResource("")).getPath() + "03.txt", "rw");
        FileChannel toChannel = bFile.getChannel();

        int position = 0;
        long size = fromChannel.size();
        fromChannel.transferTo(position, size, toChannel);

        aFile.close();
        bFile.close();
        System.out.println("over!");
    }
}
